<template>
  <div class="table-box">
    <ProTable ref="proTableRef" row-key="id" title="野保相机" :columns="columns" :request-api="loadTable" highlight-current-row>
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
      </template>
      <!-- 账户操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton>
        <ElButton type="primary" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="primary" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
      </template>
    </ProTable>
    <WildCameraForm ref="formRef" @success="refresh" />
  </div>
</template>

<script setup lang="ts" name="ZLWildCamera">
import { CirclePlus, Delete, EditPen, View } from '@element-plus/icons-vue'
import WildCameraForm from './wild-camera-form.vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { WildCamera, pageWildCamera, removeWildCamera } from '@api/tby/education/wildCamera'
import { getAllDeptTree } from '@/utils/param'
import { FormType } from '@/enums'
const proTableRef = ref<ProTableInstance>()

function refresh() {
  proTableRef.value?.getTableList()
}

// 部门树
async function treeDept() {
  const data = await getAllDeptTree()
  return { data }
}

function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

const OnlineEnum = [
  { value: 0, label: '离线' },
  { value: 1, label: '在线' }
]

// 表格列
const columns = reactive<ColumnProps<WildCamera.Record>[]>([
  { type: 'index', label: '序号', width: 60, index: getIndex },
  { prop: 'code', label: '设备编码', search: { el: 'input', key: 'code', order: 3 }, align: 'center', minWidth: 150 },
  { prop: 'name', label: '设备名称', search: { el: 'input', key: 'name' }, align: 'center', minWidth: 150 },
  {
    prop: 'deptId',
    label: '部门',
    enum: treeDept,
    search: { el: 'tree-select', key: 'deptId', order: 2, props: { checkStrictly: true } },
    fieldNames: { label: 'name', value: 'id' },
    minWidth: 150
  },
  { prop: 'category', label: '设备类型', align: 'center' },
  { prop: 'ip', label: 'ip地址', align: 'center' },
  {
    prop: 'online',
    label: '在线状态',
    enum: OnlineEnum,
    tag: true
  },
  { prop: 'longitude', label: '经度', align: 'center' },
  { prop: 'latitude', label: '纬度', align: 'center' },
  { prop: 'brand', label: '设备品牌', align: 'center' },
  { prop: 'operation', label: '操作', fixed: 'right', width: 230 }
])

const loadTable = (query: WildCamera.Query) => {
  return pageWildCamera(query)
}
// 打开 drawer(新增、查看、编辑)
const formRef = ref<InstanceType<typeof WildCameraForm> | null>(null)

// 查看用户
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, row.id)
}

// 添加用户按钮
const onAddClicked = () => {
  formRef.value?.open(FormType.ADD)
}

// 编辑账户
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 删除账户
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('您确定要删除该信息吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      removeWildCamera(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        proTableRef.value?.getTableList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'warning',
        message: '删除取消!'
      })
    })
}
</script>
